package cn.e4j.modular.system.service.impl;

import cn.e4j.modular.system.entity.SysCmp;
import cn.e4j.modular.system.entity.SysRoleCmp;
import cn.e4j.modular.system.mapper.SysRoleCmpMapper;
import cn.e4j.modular.system.service.SysRoleCmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色-UI组件关联表 服务实现类
 * </p>
 *
 * @author wenchuan.li
 * @since 2018-12-13
 */
@Service
public class SysRoleCmpServiceImpl extends ServiceImpl<SysRoleCmpMapper, SysRoleCmp> implements SysRoleCmpService {
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> getRoleCmpsList(String roleId,String moduleId){
        return this.baseMapper.getRoleCmpsList(roleId,moduleId);
    }

    @Transactional
    @Override
    public List<Map<String, String>> initRoleCmps(String roleId,String moduleId){
        this.baseMapper.deleteRoleCmps(roleId,moduleId);
        List<SysCmp> sysCmpList = this.baseMapper.selectCmpByModuleId(moduleId);
        for(SysCmp sysCmp : sysCmpList){
            SysRoleCmp sysRoleCmp = new SysRoleCmp();
            sysRoleCmp.setCMP_ID(sysCmp.getID());
            sysRoleCmp.setROLE_ID(roleId);
            sysRoleCmp.setCMP_STATUS("3");
            sysRoleCmp.setGRANT_TYPE("1");
            this.baseMapper.insert(sysRoleCmp);
        }
        return this.baseMapper.getRoleCmpsList(roleId,moduleId);
    }

    @Transactional
    @Override
    public void changeRoleCmpsStatus(List<SysRoleCmp> lists){
        for (SysRoleCmp sysRoleCmp : lists) {
            this.baseMapper.updateById(sysRoleCmp);
        }
    }
}
